//
//  DatePickerCell.m
//
//  Created by Olof Millberg on 2010-04-08.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "DatePickerCell.h"

@implementation DatePickerCell
@synthesize dateLabel;
@synthesize previousButton;
@synthesize nextButton;
@synthesize currentDate;
@synthesize delegate;
@synthesize enabled;

- (void) setEnabled:(BOOL)isEnabled
{
	enabled = isEnabled;
	self.previousButton.hidden = !enabled;
	self.nextButton.hidden = !enabled;
	self.dateLabel.enabled = enabled;
	
	if (enabled)
	{
		[dateLabel setBackgroundImage:[UIImage imageNamed:@"button_datePicker.png"] forState:UIControlStateNormal];
		[dateLabel setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
		dateLabel.titleLabel.shadowOffset = CGSizeZero;
		dateLabel.titleLabel.font = [UIFont systemFontOfSize:15];
		CGRect frame = dateLabel.frame;
		frame.size.width = 200;
		frame.origin.x = 50;
		dateLabel.frame = frame;
	}
	else 
	{
		[dateLabel setBackgroundImage:nil forState:UIControlStateNormal];
		[dateLabel setTitleColor:[UIColor colorWithRed:76.f/255 green:86.f/255 blue:110.f/255 alpha:1] forState:UIControlStateNormal];
		dateLabel.titleLabel.shadowOffset = CGSizeMake(0.0f, 1.0f);
		dateLabel.titleLabel.font = [UIFont boldSystemFontOfSize:17];
		CGRect frame = dateLabel.frame;
		frame.size.width = 300;
		frame.origin.x = 0;
		dateLabel.frame = frame;
	}

}

// Next button clicked.
- (IBAction) nextDate
{
	self.currentDate = [[[NSDate alloc] initWithTimeInterval:60*60*24 sinceDate:currentDate] autorelease];
	[delegate dateChanged:self.currentDate];
}

// Previous button clicked.
- (IBAction) previousDate
{
	self.currentDate = [[[NSDate alloc] initWithTimeInterval:-60*60*24 sinceDate:currentDate] autorelease];
	[delegate dateChanged:self.currentDate];
}

// Override setter to set the label as well.
- (void) setCurrentDate:(NSDate *)date
{
	[currentDate release];
	currentDate = [date retain];
	
	NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
	[dateFormatter setDateFormat:@"EEEE d MMMM YYYY"];
	[dateLabel setTitle:[dateFormatter stringFromDate:currentDate] forState:UIControlStateNormal];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
	self.currentDate = datePicker.date;
	[delegate dateChanged:self.currentDate];
	[datePicker release];
	[actionSheet release];
}

- (void) showDatePicker
{
	UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"\n\n\n\n\n\n\n\n\n\n\n\n"
													  delegate:self
											 cancelButtonTitle:@"OK"
										destructiveButtonTitle:nil
											 otherButtonTitles:nil];
	[actionSheet showInView:self.superview];
	
	// Add the picker
	datePicker = [[UIDatePicker alloc] init];
	datePicker.datePickerMode = UIDatePickerModeDate;
	
	datePicker.date = currentDate;
	
	[actionSheet addSubview:datePicker];
}

@end
